import logging

import allure
import jsonpath

from utils.send_request import send_jdbc_request


def json_extractor(case, global_var, res):
    # json提取
    if case["jsonExData"]:
        with allure.step("4.JSON提取"):
            for key, value in eval(case["jsonExData"]).items():
                print(key, value)
                global_var[key] = jsonpath.jsonpath(res.json(), value)[0]
            # print(global_var)
            logging.info(f"4.JSON提取，根据{case['jsonExData']}提取数据，此时全局变量为:{global_var}")


def jdbc_extractor(case, global_var):
    # 数据库提取
    if case["sqlExData"]:
        with allure.step("4.JDBC提取"):
            for key, value in eval(case["sqlExData"]).items():
                value = send_jdbc_request(value)
                global_var[key] = value
                # print(global_var)
            logging.info(f"4.JDBC提取，根据{case['sqlExData']}提取数据，此时全局变量为:{global_var}")
